package com.bmps.springcloud.insuredmicroservice.service.trial.strategy;
import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.List;
/**
 * 保费试算  HCT013
 * @author lyw
 * @date 2019-07-30 14:39:18
 */
public class Hct013TrialStrategy implements TrialStrategy {

    private static Logger logger = LoggerFactory.getLogger(Hct013TrialStrategy.class);

    @Override
    public TrialPersonModel premiumTrialCalculationPrepare(ProductModel productModel, RequestModel requestModel,
                                                           RedisService redisService){
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        for(InsurantModel insurantModel :insurantModelList){
            if (insurantModel.getInsSeq() == productModel.getProdInsSeq()) {
                Date vaildate = requestModel.getPolicyInfo().getPolApplyDate();
                String riskCode = productModel.getProdCode();
                TrialPersonModel trialPersonModel = new TrialPersonModel();
                trialPersonModel.setRiskCode(riskCode);
                StringBuilder sb = new StringBuilder(16);
                sb.append(AcceptConstant.REDIS_PREFIX_R004).append("|").append(riskCode);
                logger.info("【" + riskCode + "】【VPU】【KEY】" + sb.toString());
                Object object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                TrialUtil.setSpecialElement(trialPersonModel, object.toString());
                //0 --取报文  4 --取数据库
                if (StringUtils.equals("0", trialPersonModel.getPayEndYearRela())) {
                    trialPersonModel.setPayEndYear(productModel.getPayYear());
                    trialPersonModel.setPayEndYearFlag(null);
                }
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R006).append("|")
                        .append(trialPersonModel.getRiskCode()).append("|G");
                logger.info("【" + riskCode + "】【公式】【KEY】" + sb.toString());
                //设置险种对应的计算公式
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                trialPersonModel.setFormula(object.toString());
                trialPersonModel.setGet(productModel.getAmnt());
                //1.设置要素年龄
                trialPersonModel.setAppAge(DateUtil.getYears(insurantModel.getBirthday(), vaildate));
                //2.设置要素性别
                trialPersonModel.setSex(insurantModel.getSex());
                //3.设置要素保险期间
                trialPersonModel.setInsuYear(productModel.getInsureYear());
                String payIntv = requestModel.getProductInfo().getPayIntv();
                if (StringUtils.equals("0", payIntv)) {
                    payIntv = "12";
                }
                trialPersonModel.setPayIntv(payIntv);
                if (StringUtils.equals("1", payIntv)) {
                    trialPersonModel.setPayIntvRate("0.09");
                } else {
                    trialPersonModel.setPayIntvRate("1");
                }
                //5.设置要素险种计划版本
                trialPersonModel.setPlanVersion(productModel.getPlanVersion());
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R001).append("|")
                        .append(riskCode);
                logger.info("【"+riskCode+"】【产品计算要素配置】【KEY】"+sb.toString());
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                String result = TrialUtil.getRiskRateKey(trialPersonModel,object.toString());
                logger.info("【"+riskCode+"】【产品费率】【KEY】"+result);
                object = redisService.get(result);
                if (object == null) {
                    return new TrialPersonModel();
                }
                TrialUtil.setElementRate(trialPersonModel, object.toString());
                sb.setLength(0);
                sb.append(AcceptConstant.REDIS_PREFIX_R005).append("|").append(riskCode).append("|G");
                logger.info("【"+riskCode+"】【产品计算公式要素配置】【KEY】"+sb.toString());
                object = redisService.get(sb.toString());
                if (object == null) {
                    return new TrialPersonModel();
                }
                TrialUtil.setElementMapping(trialPersonModel, object.toString());
                return trialPersonModel;
            }
        }
        return new TrialPersonModel();
    }
}
